<template>
    <view >
        <uni-popup ref="popup_confirm" type="bottom" background-color="#fff" >
            <view class="bg-fff p-l-20 p-r-20 p-b-84 p-t-20 " style="border-radius:30rpx;" v-if="openType == 1">
                <view class="c_31b fz-16 m-b-12">请确认购买</view>
                <view >
                    <view class="b-b-1 view flex-row align-center justify-between h41">
                        <view class="c_678 fz-14 ">购买NFT产品</view>
                        <view class="fz-14 c_31b fz-14">{{ params.name }}</view>
                    </view>

                    <view class="b-b-1 view flex-row align-center justify-between h41">
                        <view class="c_678 fz-14">购买数量</view>
                        <view class="c_31b fz-14">{{ params.number }}</view>
                    </view>

                    <view class="b-b-1 view flex-row align-center justify-between h41">
                        <view class="c_678 fz-14">花费总价</view>
                        <view class="c_31b fz-14">{{ params.pay_amount}} {{ params.token}}</view>
                    </view>
					<view class="b-b-1 view flex-row align-center justify-between h41">
					    <view class="c_678 fz-14">可用余额</view>
					    <view class="c_31b fz-14">{{ balance}} {{ params.token}}</view>
					</view>
					<view class="b-b-1 view flex-row align-center justify-between h41">
					    <view class="c_678 fz-14">Gas</view>
					    <view class="c_31b fz-14">{{showGas }}
							<view v-if="gasFull == 0">（余额不足Gas）</view>
						</view>
					</view>
                </view>

                <view class="m-b-12 b-b-1">
                    <uniEasyinput :inputBorder="false" v-model="passWord" type="password"  placeholder="请输入密码" class="h41 width100" style="border:0;" />
                </view>

                <el-button class=" h44 view align-center justify-center" block style="border-radius: 12px !important;width:100%;display:flex !important;font-size:30rpx;" type="primary" @click="confirm">确认</el-button>
            </view>

            <view class="bg-fff p-l-20 p-r-20 p-b-84 p-t-20 " style="border-radius:30rpx;" v-else-if="openType == 2">
                <view class="c_31b fz-16 m-b-12">请确认质押</view>
                <view >
                    <view class="b-b-1 view flex-row align-center justify-between h41">
                        <view class="c_678 fz-14 ">质押通证数量</view>
                        <view class="fz-14 c_31b fz-14">{{ params.pay_amount }} {{ params.token}}</view>
                    </view>

                    <view class="b-b-1 view flex-row align-center justify-between h41">
                        <view class="c_678 fz-14">质押NFT算力</view>
                        <view class="c_31b fz-14">{{ params.power }}</view>
                    </view>

                    <view class="b-b-1 view flex-row align-center justify-between h41">
                        <view class="c_678 fz-14">质押NFT详情</view>
                        
                        <view class="view flex-row align-center flex-wrap">
                            <view v-for="(item,index) in params.selectNFT" :key="index" class="m-r-5 c-122 fz-10">{{ item.nft_name }} * {{ item.numVal }};</view>
                        </view>
                    </view>
					
					<view class="b-b-1 view flex-row align-center justify-between h41">
					    <view class="c_678 fz-14">可用余额</view>
					    <view class="c_31b fz-14">{{balance}} {{ params.token}}</view>
					</view>
					<view class="b-b-1 view flex-row align-center justify-between h41">
					    <view class="c_678 fz-14">Gas</view>
					    <view class="c_31b fz-14">{{showGas}}
							<view v-if="gasFull == 0">（余额不足Gas）</view>
						</view>
					</view>
                </view>

                <view class="m-b-12 b-b-1">
                    <uniEasyinput :inputBorder="false" type="password" v-model="passWord" placeholder="请输入密码" class="h41 width100" style="border:0;" />
                </view>

                <el-button @click="confirm" class=" h44 view align-center justify-center" block style="border-radius: 12px !important;width:100%;display:flex !important;font-size:30rpx;" type="primary">确认</el-button>
            </view>
			
			<view class="bg-fff p-l-20 p-r-20 p-b-84 p-t-20 " style="border-radius:30rpx;" v-else-if="openType == 3">
			    <view class="c_31b fz-16 m-b-12">请输入您想创建的社区名称 <br />（创建后需要后台审核，失败不扣款）</view>
			    <view >
					<view class="b-b-1 view flex-row align-center justify-between h41">
					    <view class="c_678 fz-14 ">社区名称</view>
					    <view class="fz-14 c_31b fz-14">{{ params.name}}</view>
					</view>
			        <view class="b-b-1 view flex-row align-center justify-between h41">
			            <view class="c_678 fz-14 ">需要金额</view>
			            <view class="fz-14 c_31b fz-14">{{ params.pay_amount}}  {{ params.token}}</view>
			        </view>
			        <view class="b-b-1 view flex-row align-center justify-between h41">
			            <view class="c_678 fz-14">可用余额</view>
			            <view class="c_31b fz-14">{{balance}} {{params.token}}</view>
			        </view>
					<view class="b-b-1 view flex-row align-center justify-between h41">
					    <view class="c_678 fz-14">Gas</view>
					    <view class="c_31b fz-14">{{showGas }}
							<view v-if="gasFull == 0">（余额不足Gas）</view>
						</view>
					</view>
			    </view>
				<!--<view class="m-b-12 b-b-1">
					<uniEasyinput :inputBorder="false" placeholder="社区名称" class="h41 width100" style="border:0;" />
				</view>-->
			    <view class="m-b-12 b-b-1">
			        <uniEasyinput :inputBorder="false" v-model="passWord" type="password" placeholder="请输入密码" class="h41 width100" style="border:0;" />
			    </view>
			
			    <el-button class=" h44 view align-center justify-center" block style="border-radius: 12px !important;width:100%;display:flex !important;font-size:30rpx;" type="primary" @click="confirm">确认</el-button>
			</view>
			
			<view class="bg-fff p-l-20 p-r-20 p-b-84 p-t-20 " style="border-radius:30rpx;" v-else-if="openType == 4">
			    <view class="c_31b fz-16 m-b-12">正在领取收益，请确认</view>
			    <view >
			        <view class="b-b-1 view flex-row align-center justify-between h41">
			            <view class="c_678 fz-14">可用余额</view>
			            <view class="c_31b fz-14">{{balance}} {{params.token}}</view>
			        </view>
					<view class="b-b-1 view flex-row align-center justify-between h41">
					    <view class="c_678 fz-14">Gas</view>
					    <view class="c_31b fz-14">{{showGas }}
							<view v-if="gasFull == 0">（余额不足Gas）</view>
						</view>
					</view>
			    </view>
				<!--<view class="m-b-12 b-b-1">
					<uniEasyinput :inputBorder="false" placeholder="社区名称" class="h41 width100" style="border:0;" />
				</view>-->
			    <view class="m-b-12 b-b-1">
			        <uniEasyinput :inputBorder="false" v-model="passWord" type="password" placeholder="请输入密码" class="h41 width100" style="border:0;" />
			    </view>
			
			    <el-button class=" h44 view align-center justify-center" block style="border-radius: 12px !important;width:100%;display:flex !important;font-size:30rpx;" type="primary" @click="confirm">确认</el-button>
			</view>
			
			<view class="bg-fff p-l-20 p-r-20 p-b-84 p-t-20 " style="border-radius:30rpx;" v-else-if="openType == 5">
			    <view class="c_31b fz-16 m-b-12">正在退出贡献池，请确认</view>
			    <view >
			        <view class="b-b-1 view flex-row align-center justify-between h41">
			            <view class="c_678 fz-14">可用余额</view>
			            <view class="c_31b fz-14">{{balance}} {{params.token}}</view>
			        </view>
					<view class="b-b-1 view flex-row align-center justify-between h41">
					    <view class="c_678 fz-14">Gas</view>
					    <view class="c_31b fz-14">{{showGas }}
							<view v-if="gasFull == 0">（余额不足Gas）</view>
						</view>
					</view>
			    </view>
				<!--<view class="m-b-12 b-b-1">
					<uniEasyinput :inputBorder="false" placeholder="社区名称" class="h41 width100" style="border:0;" />
				</view>-->
			    <view class="m-b-12 b-b-1">
			        <uniEasyinput :inputBorder="false" v-model="passWord" type="password" placeholder="请输入密码" class="h41 width100" style="border:0;" />
			    </view>
			
			    <el-button class=" h44 view align-center justify-center" block style="border-radius: 12px !important;width:100%;display:flex !important;font-size:30rpx;" type="primary" @click="confirm">确认</el-button>
			</view>
			
            <view class="bg-fff p-l-20 p-r-20 p-b-84 p-t-20 " style="border-radius:30rpx;" v-else>
                <view class="c_31b fz-16 m-b-12">确认您的账户型号</view>
                <view >
                    <view class="b-b-1 view flex-row align-center justify-between h41">
                        <view class="c_678 fz-14 ">激活账户</view>
                        <view class="fz-14 c_31b fz-14">{{ params.pay_amount}}</view>
                    </view>
                    <view class="b-b-1 view flex-row align-center justify-between h41">
                        <view class="c_678 fz-14">可用余额</view>
                        <view class="c_31b fz-14">{{balance}} {{params.token}}
							<view v-if="gasFull == 0">（余额不足Gas）</view>
						</view>
                    </view>
                </view>

                <view class="m-b-12 b-b-1">
                    <uniEasyinput :inputBorder="false" v-model="passWord" placeholder="请输入密码" class="h41 width100" style="border:0;" />
                </view>

                <el-button class=" h44 view align-center justify-center" block style="border-radius: 12px !important;width:100%;display:flex !important;font-size:30rpx;" type="primary">确认</el-button>
            </view>
        </uni-popup>
    </view>
</template>

<script>
import uniEasyinput from '@/components/uni-easyinput/uni-easyinput'
import uniPopup from '@/components/uni-popup/uni-popup'
import elButton from '@/components/el-button/el-button'

import { aboutWallet } from '@/common/businessCommon'
import { mapMutations, mapState } from "vuex";
import {getTokenBalance} from "@/apis/bsc20.js";
import {getConfig} from "@/apis/wallet.js"

export default {
    // openType == 1 购买nft type == 2质押确认 其他激活账号
    props: {
        openType: {
            type: [Number,String],
            default: ''   
        },
        params: {
            type: Object,
            default: () => {
                return {
					token:"YPT",
                    selectNFT: []
                }
            }  
        }
    },
    components: {
        uniPopup,
        uniEasyinput,
        elButton
	},
    data() {
        return {
            passWord: '',
			balance: 0,
			gasFull: 1,
			showGas: '',
			config:{},
        }
    },
	async created(){
		//this.defaultWallet.address = '0x6c2538b0df993eacc5709b3667d0afa9b065478a';
		if(!this.params.token){
			this.params.token = 'CHB'
		}
		console.log('created',this.params.token)
		await this.checkBalance();
		this.getGas();
		await this.checkGas();
	},
	onShow() {
	    console.log('show')
	},
	async onLoad(){
		
	},
    computed: {
        ...mapState(["defaultWallet"]),
    },
    methods: {
        async confirm() {
			//this.params.key=1;
			//this.$emit('confirm')
			//return true;
			
            if (!this.passWord) {
                uni.showToast({
                    title: '请输入密码',
                    icon: 'none'
                })
                return false
            }
			//await this.checkGas()
			if(this.gasFull == 0){
				uni.showToast({
				    title: 'Gas不足',
				    icon:'none'
				})
				return false
			}
			console.log('check balance',this.params.pay_amount,this.balance)
			if(Number(this.params.pay_amount) > Number(this.balance)){
				uni.showToast({
				    title: '余额不足',
				    icon:'none'
				})
				return false
			}
			console.log('检查密码');
            const ret = await aboutWallet.checkPassword(this.defaultWallet.address,this.passWord)
            if (ret) {
				//let walletInfo = await aboutWallet.getWalletInfo(this.defaultWallet.address, this.password);
				//this.params.key = walletInfo.privateKey;
                const re = await aboutWallet.getWalletPrivateKey(this.defaultWallet.address, this.passWord, true)
				this.params.key = re;
				console.log('密码通过');
                this.$emit('confirm')
            } else {
                this.passWord = ''
                uni.showToast({
                    title: '密码错误,请重新输入',
                    icon:'none'
                })
            }
        },
		async checkBalance(){
			//做不到在这里检查，提交之前传递吧
			console.log('params token',this.params.token)
			let balance = await getTokenBalance(this.defaultWallet.address, this.params.token)
			if(balance.data.amount){
				this.balance = balance.data.amount
			}
		},
		async getGas(){
			// 第一步 拿配置
			let c = await getConfig();
			this.config = c.data;
			this.showGas = this.config.transfer_fee + ' ' + this.config.transfer_token;
		},
		async checkGas(){
			// 第二步 判断金额，如果为普通代笔单独运算gas是否充足
			console.log('check',this.params.token)
			let balance = await getTokenBalance(this.defaultWallet.address, 'chb')
			if(this.params.token.toLowerCase() != 'chb'){
				let amount = 0;
				if(balance.data.amount){
					amount = Number(balance.data.amount)
				}
				if(amount < this.config.transfer_fee){
					this.gasFull = 0;
				}
				console.log('gas检查1',this.config.transfer_fee,amount);
			}
			
			// 第三步 如果为chb，则加上pay_amount 一起判断gas是否充足
			if(this.params.token.toLowerCase() == 'chb' && this.params.pay_amonut){
				if(amount < this.config.transfer_fee + this.params.pay_amonut){
					this.gasFull = 0;
				}
				console.log('gas检查2');
			}
			console.log('gas检查',this.gasFull);
		},
        onShowDialog() {
           this.$refs.popup_confirm.open()
        },
		onClose(){
			this.$refs.popup_confirm.close()
		}
    }
    
};
</script>

<style lang="scss" scoped>
.b-b-1 {border-bottom: 1px solid #EDEDED;}

</style>
